﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace AGOS
{
    public partial class Config : Form
    {
        public Config()
        {
            InitializeComponent();
            FillForm();
        }


          private bool saved = false;
       

        private void FillForm()
        {
            try
            {
                textBoxLog.Text = ConfigurationManager.AppSettings["LOG"];
                textBoxconnectionString.Text = ConfigurationManager.AppSettings["Connection_String"];
                txtEmailStatus.Text = ConfigurationManager.AppSettings["StatusEmail"];
                txtSMTP.Text = ConfigurationManager.AppSettings["SMTP_Server"];
                txtPORT.Text = ConfigurationManager.AppSettings["Port"];
                txtEmailSender.Text = ConfigurationManager.AppSettings["Sender"];
                txtPasswordSender.Text = ConfigurationManager.AppSettings["Password"];
                textBoxExcelFIle.Text = ConfigurationManager.AppSettings["ExcelPath"];
                
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SSL"]))
                {
                    if (ConfigurationManager.AppSettings["SSL"].ToLower().Equals("y"))
                        comboSSL.SelectedIndex = 0;
                    else
                        comboSSL.SelectedIndex = 1;
                }

                string time = ConfigurationManager.AppSettings["Start_Time"];
                string[] timesplit = time.Split(new char[] { ':' });
                comboBoxHour.SelectedItem = timesplit[0];
                comboBoxMin.SelectedItem = timesplit[1];
          
            }
            catch (Exception ex) { MessageBox.Show("Errore durante la lettura dal file di configurazione: " + Environment.NewLine + ex.Message); }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (
                string.IsNullOrEmpty(textBoxLog.Text.Trim())
                || string.IsNullOrEmpty(textBoxconnectionString.Text.Trim())
                || string.IsNullOrEmpty(txtEmailSender.Text.Trim())
                || string.IsNullOrEmpty(txtEmailStatus.Text.Trim())
                || string.IsNullOrEmpty(txtPasswordSender.Text.Trim())
                || string.IsNullOrEmpty(txtPORT.Text.Trim())
                || string.IsNullOrEmpty(txtSMTP.Text.Trim())
                || string.IsNullOrEmpty(comboSSL.Text.Trim())
                )
            {
                MessageBox.Show("Si prega di compilare o selezionare tutti campi.", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(comboBoxMin.Text.ToString())
                || string.IsNullOrEmpty(comboBoxHour.Text.ToString())
             
                )
            {
                MessageBox.Show("Si prega di selezionare il tutti campi per ora d'inizio", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            System.Configuration.Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfg.AppSettings.Settings["Start_Time"].Value = comboBoxHour.SelectedItem.ToString() + ":" + comboBoxMin.SelectedItem.ToString();
            cfg.AppSettings.Settings["LOG"].Value = textBoxLog.Text + "\\";
            cfg.AppSettings.Settings["StatusEmail"].Value = txtEmailStatus.Text.Trim();
            cfg.AppSettings.Settings["Connection_String"].Value = textBoxconnectionString.Text.Trim();
            cfg.AppSettings.Settings["SMTP_Server"].Value = txtSMTP.Text.Trim();
            cfg.AppSettings.Settings["Port"].Value = txtPORT.Text.Trim();
            cfg.AppSettings.Settings["Sender"].Value = txtEmailSender.Text.Trim();
            cfg.AppSettings.Settings["Password"].Value = txtPasswordSender.Text.Trim();
            cfg.AppSettings.Settings["ExcelPath"].Value = textBoxExcelFIle.Text.Trim();
            cfg.AppSettings.Settings["Monday"].Value = Monday.Checked.ToString();
            cfg.AppSettings.Settings["Tuesday"].Value = Tuesday.Checked.ToString();
            cfg.AppSettings.Settings["Wednesday"].Value = Wednesday.Checked.ToString();
            cfg.AppSettings.Settings["Thursday"].Value = Thursday.Checked.ToString();
            cfg.AppSettings.Settings["Friday"].Value = Friday.Checked.ToString();
            cfg.AppSettings.Settings["Saturday"].Value = Saturday.Checked.ToString();
            cfg.AppSettings.Settings["Sunday"].Value = Sunday.Checked.ToString();

            if (comboSSL.Text.Equals("Si"))
                cfg.AppSettings.Settings["SSL"].Value = "Y";
            else
                cfg.AppSettings.Settings["SSL"].Value = "N";
            cfg.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            MessageBox.Show("Configurazione salvata");

            saved = true;
            return;
        }

        private void Confg_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (saved == false)
            {
                DialogResult rez = MessageBox.Show("Volete salvare le modifiche?", "Attenzione", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);

                switch (rez)
                {
                    case DialogResult.Yes:
                        button1_Click(sender, new EventArgs());
                        break;
                    case DialogResult.No:
                        break;
                    case DialogResult.None:
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }

        private void buttonLog_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            if (fbd.SelectedPath != "")
            {
                textBoxLog.Text = fbd.SelectedPath + "\\";
            }
        }

        /// <summary>
        /// refresh the user list every time the configuration panel is opened
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfgEstrazionePratiche_Load(object sender, EventArgs e)
        {
        }

        private void buttonExcel_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (DialogResult.OK == fbd.ShowDialog())
            {
                textBoxExcelFIle.Text = fbd.SelectedPath+"\\";
            }
        }
    }
}
